@extends('admin::layouts.master')

@section('title')
    权限管理
@endsection

@section('nav')
    <div class="row mb-2">
        <div class="col-sm-6">
            <h1 class="m-0">权限管理</h1>
        </div>
        <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
                <li class="breadcrumb-item"><a href="{{route('admin.permission')}}">权限管理</a></li>
                <li class="breadcrumb-item active">列表</li>
            </ol>
        </div>
    </div>

@endsection

@section('content')
    <div class="card">

        <div class="card-header">
            <a class="btn btn-sm btn-info" href="{{route('admin.permission.create')}}">
                添加权限
            </a>
        </div>

        <div class="card-body">
            <table class="table table-hover">
                <tbody>
                @foreach($permissions as $permission)
                    @if(count($permission->allChild))
                        <tr data-widget="expandable-table" aria-expanded="false">
                            <td>
                                <i class="fas fa-caret-right fa-fw"></i>
                                {{$permission->display_name}}

                                <div class="btn-group float-right">
                                    <a href="{{route('admin.permission.edit',['id'=>$permission->id])}}" class="btn btn-xs btn-info">编辑</a>
                                    <span class="btn btn-xs btn-danger" data-permission-id="{{$permission->id}}" onclick="delPermission('{{$permission->id}}',this)">删除</span>
                                </div>
                            </td>
                        </tr>
                        <tr class="expandable-body">
                            <td>
                                <div class="p-0" style="">
                                    <table class="table table-hover">
                                        <tbody>
                                        @foreach($permission->allChild as $subPermission)
                                            @if(count($subPermission->allChild)>0)
                                                <tr data-widget="expandable-table" aria-expanded="false">
                                                    <td>
                                                        <i class="fas fa-caret-right fa-fw"></i>
                                                        {{$subPermission->display_name}}

                                                        <div class="btn-group float-right">
                                                            <a href="{{route('admin.permission.edit',['id'=>$subPermission->id])}}" class="btn btn-xs btn-info">编辑</a>
                                                            <span  data-permission-id="{{$subPermission->id}}" onclick="delPermission('{{$subPermission->id}}',this)" class="btn btn-xs btn-danger">删除</span>
                                                        </div>
                                                    </td>
                                                </tr>
                                                <tr class="expandable-body">
                                                    <td>
                                                        <div class="p-0" style="">
                                                            <table class="table table-hover">
                                                                <tbody>
                                                                @foreach($subPermission->allChild as $child)
                                                                    <tr>
                                                                        <td data-id="{{$child->id}}">
                                                                            <div class="btn-group" style="width: 200px;">
                                                                                {{$child->display_name}}
                                                                            </div>
                                                                            <div class="btn-group  float-right">
                                                                                <a href="{{route('admin.permission.edit',['id'=>$child->id])}}" class="btn btn-xs btn-info">编辑</a>
                                                                                <span  data-permission-id="{{$child->id}}" onclick="delPermission('{{$child->id}}',this)"  class="btn btn-xs btn-danger">删除</span>
                                                                            </div>
                                                                        </td>
                                                                    </tr>
                                                                @endforeach
                                                                </tbody>
                                                            </table>
                                                        </div>
                                                    </td>
                                                </tr>
                                            @else
                                                <tr>
                                                    <td data-id="{{$subPermission->id}}">
                                                        {{$subPermission->display_name}}
                                                    <div class="btn-group  float-right">
                                                        <a href="{{route('admin.permission.edit',['id'=>$child->id])}}" class="btn btn-xs btn-info">编辑</a>
                                                        <span  data-permission-id="{{$subPermission->id}}" onclick="delPermission('{{$subPermission->id}}',this)"  class="btn btn-xs btn-danger">删除</span>
                                                    </div>
                                                    </td>
                                                </tr>
                                            @endif

                                        @endforeach
                                        </tbody>
                                    </table>
                                </div>
                            </td>
                        </tr>

                    @else
                        <tr>
                            <td data-id="{{$permission->id}}">
                                {{$permission->display_name}}
                                <div class="btn-group float-right">
                                    <a href="{{route('admin.permission.edit',['id'=>$permission->id])}}" class="btn btn-xs btn-info">编辑</a>
                                    <span data-permission-id="{{$permission->id}}" onclick="delPermission('{{$permission->id}}',this)" class="btn btn-xs btn-danger">删除</span>
                                </div>
                            </td>
                        </tr>
                    @endif
                @endforeach
                </tbody>
            </table>
        </div>
    </div>

@endsection


@section('js')
    <script>
        function delPermission(permissionId,obj){
            if (confirm('您确定删除该权限吗?')){
                $.post("{{ route('admin.permission.destroy')}}", {
                    _method: 'delete',
                    ids: [permissionId]
                }, function (res) {
                    if (res.code == 0){
                        toastr.success('删除成功');
                        $(obj).parent().parent().parent('tr').remove();
                    }else{
                        toastr.error(res.msg);
                    }
                });
            }
        }
    </script>

@endsection